package com.dhsc.service.impl.ywzj.kbpzgl.pzlxwh;

import com.dhsc.domain.ywzx.kbpzgl.PzgllxxxVo;
import com.dhsc.domain.ywzx.kbpzgl.PzlxwhVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywzj.kbpzgl.pzlxwh.IPzlxwhDao;
import com.dhsc.service.ywzj.kbpzgl.pzlxwh.IPzlxwhService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 凭证类型维护
 * @date 2024/4/9 15:47
 */
@Service
@Slf4j
public class PzlxwhServiceImpl implements IPzlxwhService {

    @Autowired
    private IPzlxwhDao pzlxwhDao;

    @Override
    public LayuiTableVo queryPzlxwhinfo(PzlxwhVo p) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            if(!CommonUtil.isNotEmptyObj(p.getState())){
                PageMethod.startPage(p.getPage(), p.getLimit());
            }
            List<PzlxwhVo> list = pzlxwhDao.queryPzlxwhinfo(p);
            PageInfo<PzlxwhVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询凭证类型维护出错",e);
            throw new MyException("查询凭证类型维护出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject savePzlxwhFile(PzlxwhVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            if(CommonUtil.isNotEmptyObj(p.getWjlj()) && CommonUtil.isNotEmptyObj(p.getWjmc())){
                p.setWjlj(p.getWjlj()+"/"+p.getWjmc());
            }
            if(!CommonUtil.isNotEmptyObj(p.getPzbh())){
                //新增
                p.setTybz("0");
                p.setTyrq("30000101");
                pzlxwhDao.savePzlxwhxx(p);
                //查询最新新增的id
                PzlxwhVo pz = pzlxwhDao.queryPzlxNewId();
                p.setPzbh(pz.getPzbh());
                pzlxwhDao.savePzlxwhWjxx(p);
            }else {
                pzlxwhDao.updatePzlxwhxx(p);
                if(CommonUtil.isNotEmptyObj(p.getWjlj())){
                    pzlxwhDao.updatelxwhWjxx(p);
                }
            }
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存凭证类型维护出错",e);
            throw new MyException("保存凭证类型维护出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject deletePzlx(PzlxwhVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //删除
            pzlxwhDao.deletePzlx(p);
            pzlxwhDao.deletePzlxWj(p);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("删除凭证类型出错",e);
            throw new MyException("删除凭证类型出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryPzgllxxx(PzgllxxxVo p) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            if(!CommonUtil.isNotEmptyObj(p.getState())){
                PageMethod.startPage(p.getPage(), p.getLimit());
            }
            List<PzgllxxxVo> list = pzlxwhDao.queryPzgllxxx(p);
            PageInfo<PzgllxxxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
            obj.setMsg("");
        }catch (Exception e){
            log.error("查询凭证管理联系信息出错",e);
            throw new MyException("查询凭证管理联系信息出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject savePzgllxxx(PzgllxxxVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            //修改凭证管理联系信息
            pzlxwhDao.updatePzgllxxx(p);
            //往短信发送表新增数据
            String dxnr = "您好！东海证券"+p.getBmmc()+"凭证邮寄联系信息更新为："+p.getSqr()+","+p.getSj()+","+p.getLxdz()+",邮编"+p.getYzbm()+"。请及时更新！";
            p.setDxnr(dxnr);
            pzlxwhDao.insertDxfsnr(p);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存凭证管理联系信息出错",e);
            throw new MyException("保存凭证管理联系信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryYwzxPzglyyb(PzgllxxxVo p) {
        ResponseObject obj = new ResponseObject();
        try {
            List<PzgllxxxVo> list = pzlxwhDao.queryYwzxPzglyyb(p);
            obj.setData(list);
            obj.setStatus(200);
        }catch (Exception e){
            log.error("保存凭证管理联系信息出错",e);
            throw new MyException("保存凭证管理联系信息出错",e);
        }
        return obj;
    }
}
